package com.zh.freechat.domain.message.repository.real;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.zh.freechat.domain.message.entity.ChatRelationPO;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author ZH
 * @date 23:21 2021/5/15
 */
@Mapper
public interface ChatRelationPoMapper extends BaseMapper<ChatRelationPO> {

    /**
     * 根据user查询聊天关系list
     *
     * @param user
     * @return
     */
    @Select(value = "SELECT * FROM ch_chat_relation WHERE user1=#{user} OR user2=#{user} ORDER BY last_time DESC LIMIT 1000 OFFSET 0")
    List<ChatRelationPO> findAllByUser(@Param("user") String user);

    @Insert(value = """
            INSERT INTO ch_chat_relation (id, last_time, user1, user2, create_time,cleared_u1,cleared_u2) 
             VALUES (#{relId}, #{lastTime}, #{user1}, #{user2}, NOW(), 0, 0) 
             ON DUPLICATE KEY UPDATE last_time = VALUES(last_time)
            """)
    void updateLastTimeById(@Param("relId") String relId, @Param("lastTime") LocalDateTime lastTime, @Param("user1") String user1, @Param("user2") String user2);

}
